/*
 * Copyright (C) 2015 truth
 *
 * This program is non-free software, but you can redistribute it and/or modify
 * it if you are in hjj(Huangshi Hejianjiao) team.
 */
package com.truthbean.jutils.web.utils;

/**
 * @since Jan 15, 2015 11:21:31 AM
 * @author TruthBean
 */
public class PageUtil {

    // 这个类是根据当前页，总记录数以及每页记录数创建page对象

    public static Page createPage(int size, int currentpage, int totalcounts) {
        int totalpages = totalPages(size, totalcounts);
        int prepage = prePage(currentpage);
        int nextpage = nextPage(currentpage, totalpages);
        int beginindex = beginIndex(size, currentpage);
        int lastindex = lastIndex(beginindex, size);
        return new Page(currentpage, size, totalcounts, totalpages, prepage,
                nextpage, beginindex, lastindex);
    }

    // 返回总页页数
    public static int totalPages(int size, int totalcounts) {
        if (totalcounts == 0) {
            return 1;
        } else {
            if (totalcounts % size == 0) {
                return totalcounts / size;
            }
            return totalcounts / size + 1;
        }
    }

    // 返回上一�?
    public static int prePage(int currentpage) {
        return currentpage == 1 ? 1 : (currentpage - 1);
    }

    // 返回下一�?
    public static int nextPage(int currentpage, int totalpages) {
        return currentpage == totalpages ? totalpages : (currentpage + 1);
    }

    // 返回起始查询的地�?
    public static int beginIndex(int size, int currentpage) {
        if(currentpage==0){
            return currentpage * size;
        }
        return (currentpage - 1) * size;
    }

    public static int lastIndex(int beginIndex, int size) {
        return beginIndex + size;
    }
}
